package com.bos.service.impl;

import com.bos.data.repositories.jpa.CustomerJPARepository;
import com.bos.data.model.AccessTokenModel;
import com.bos.data.model.BosCustomerModel;
import com.bos.mapper.WeiMapper;
import com.bos.service.WeiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("weiServiceImpl")
public class WeiServiceImpl implements WeiService {
    @Autowired
    WeiMapper weiMapper;
    @Autowired
    private CustomerJPARepository customerJPARepository;
    //1.将微信公众平台的Access_token 插入到AccessToken中去
    @Override
    public int insertAccessToken(AccessTokenModel access) {

        return weiMapper.insertAccessToken(access);
    }

    //根据type查询出数据
    @Override
    public List<AccessTokenModel> findAccessToken(String type) {
        // TODO Auto-generated method stub
        return weiMapper.findAccessToken(type);
    }

    @Override
    public int updateByExpirIn(AccessTokenModel acce) {
        // TODO Auto-generated method stub
        return weiMapper.updateByExpirIn(acce);
    }
    @Transactional
    @Override
    public void insertCustomer(String openId,String cname){
        BosCustomerModel model=new BosCustomerModel();
        model.setCname(cname);
        model.setOpenId(openId);
        int row=customerJPARepository.findBosCustomerModelByOpenId(openId);
        if(row==0){
            customerJPARepository.saveAndFlush(model);
        }


    }
    @Override
    public BosCustomerModel findBosCustomerModelByOpenId(String openId){
        return weiMapper.findByOpenId(openId);
    }

}
